package com.zjj.xiancheng.sync.ticket;

import java.util.concurrent.TimeUnit;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class Ticket implements Runnable {

    private int num = 100;

    public void sellTicket(){
        synchronized (this){
            if(num>=1){
                System.out.println(Thread.currentThread().getName() +" , 卖出票号："+num);
                num--;
            }
        }
    }

    @Override
    public void run() {
        while (true){
            if(num<=0){
                break;
            }
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            sellTicket();
        }
    }
}
